package codename.carrot.bomberman.models;

import sheep.game.Sprite;
import codename.carrot.bomberman.AppStorage;
import codename.carrot.bomberman.BitmapFactory;
import codename.carrot.bomberman.controllers.BombController;
import codename.carrot.bomberman.game.listener.ObstacleListener;

/**
 * Class for objects of type Bomb.
 * @author Group A4
 * 
 * @see BombController The controller for this model 
 *
 */
public class Bomb extends AbstractAnimatedObstacle { 
	/**
	 * Timer for when the bomb was created 
	 */
    public long timeOfCreation;
    /**
     * Reference to the player that placed the bomb. 
     */
    private Player owner;
    /**
     * Check for whether the bomb is animating. 
     */
    private boolean animating; //henger dette igjen fra bombkick/throw?
    /**
     * Time since the last animation. 
     */
    public long lastAnimationTime; //henger dette igjen fra bombkick/throw?
	
    /**
     * Class constructor. Sets parameters like bomb owner and time of creation. 
     * @param xCoor - X-coordinates where the bomb will be placed.
     * @param yCoor - Y-coordinates where the bomb will be placed. 
     * @param owner - Player that placed the bomb.
     * @param listener - Attached listener.
     */
	public Bomb(int xCoor, int yCoor, Player owner, ObstacleListener listener) {
		super(xCoor, yCoor, BitmapFactory.buildBombView(), listener);
		timeOfCreation = System.currentTimeMillis();		
		this.owner = owner;
		update(0f);
		setVisible(true);
		animating = false;
	}
	
	
	/**
	 * Can be used to check that the bomb can be exploded. Always true. 
	 */
	@Override
    public boolean isDestructible() {
        return true;
    }


	
	/**
	 * Update function, overrides the Sprite's default update. Discovers when the bomb should blow up.
	 */
	@Override
	public void update(float dt) {
	    super.update(dt);	    
	    if(System.currentTimeMillis() - timeOfCreation > AppStorage.BOMB_FUSE && animating == false) { 
	        fireObstacleExplodes();
	        fireRemoveObstacle();
	    }
	}
	
	/**
	 * Collision detection for when bombs interacts with other game elements. 
	 * @param otherSprite - the sprite that collided with the bomb. 
	 */
	@Override
	public boolean collides(Sprite otherSprite) {
		if (otherSprite == null)
			return false;
		return super.collides(otherSprite);
	}
	
	/**
	 * Getter for the player that owns the bomb. 
	 * @return owner - the player that placed the bomb. 
	 */
	public Player getBombOwner() {
	    return owner;
	}
	
	/**
	 * Getter for the bomb's time of creation
	 * @return timeOfCreation - time when the bomb was created
	 */
	public long getTimeOfCreation(){
		return timeOfCreation;
	}
}
